<?php
defined('IN_MAIN') or exit('Invalid Access!');

function prepare_prod_for_display($prods){
/*
This function prepare the $prods for formatting later
input $prods must have the format and the refno must be escaped
$prods[index][refno]
$prods[index][qty]

and the output will be

$prods[index][product detail]
if product is a jumpstart package, then it will be
$prods[index][product detail]
$prods[index][pck_prod][index2][product detail]

product detail includes id, refno, title, bv, dp, rp, qty, prod_type (p/n) unescaped
*/

	global $vars;
	$db=$vars["db"];

	$points=array("bv","dp","rp");
	for($i=0,$t=count($prods);$i<$t;$i++){
		$this_prod=@mysql_fetch_assoc(mysql_query("select * from $db->products where refno='".$prods[$i]["refno"]."'")) or false;
		if($this_prod){
			$dprods[$i] = $this_prod;
			$dprods[$i]["qty"] = $prods[$i]["qty"];
			//jumpstart package
			if($this_prod["prod_type"]=="p"){
				foreach($points as $point_type){
					if($this_prod[$point_type."_fix"]!='y'){
						$dprods[$i][$point_type]=calc_package_point($point_type, $this_prod["id"]);
					}
				}
				$t2=@mysql_num_rows($r2=mysql_query("select p.*, j.qty as pck_qty from $db->package_detail j, $db->products p where j.pkid='$this_prod[id]' and j.pid=p.id"));
				for($j=0;$j<$t2;$j++){
					$this_pck_prod=mysql_fetch_assoc($r2);
					$dprods[$i]["pck_prod"][$j] = $this_pck_prod;
					$dprods[$i]["pck_prod"][$j]['qty'] = $this_pck_prod['pck_qty'];
				}
			}
		}
	}

	return $dprods;
}
?>